package automatyzacjaPageObject;

import static org.junit.Assert.fail;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public abstract class Page {
	
	protected WebDriver driver;

	public Page(WebDriver driver) {
		this.driver=driver;
	}

	public void open(String url) {
		driver.get(url+"/");
	}
	
	public boolean isTextPresent(String searchedText) {
		return driver.getPageSource().contains(searchedText);
	}
	
	public void selectElementFromList(String listId, String elementName) {
		WebElement e = driver.findElement(By.id(listId));
		Select s = new Select(e);
		s.selectByVisibleText(elementName);
	}
	
	public void clickLinkByText(String link) {
		driver.findElement(By.linkText(link)).click();
	}
	
	public void clickLinkByClassName(String className) {
		driver.findElement(By.className(className)).click();
	}
	
	public boolean waitForElement(Integer numer) throws InterruptedException {
		for (int second = 0;; second++) {
			if (second >= 15) {
				fail("timeout");
				return false;
			}
			try { 
				if (isTextPresent("MyStory_"+numer)) {
					return true;
				}
	
			} 
			catch (Exception e) {
					
			}
			Thread.sleep(1000);
		}		
	}
}
